package com.platform.article.model;

import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.article.dto.ArticleDTO;
import com.platform.article.dto.EditDTO;
import com.platform.article.dto.PublishDTO;
import com.platform.article.vo.ArticleUserInfoVO;
import com.platform.article.vo.ArticleVO;
import com.platform.article.vo.ArticleInfoVO;
import com.platform.common.entity.BaseDO;
import lombok.*;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;

/**
 * @author shuc
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@TableName(ArticleDOField.TABLE_NAME)
public class ArticleDO extends BaseDO {
    @TableId(value = ArticleDOField.ID, type = IdType.ASSIGN_ID)
    private String id;

    @TableField(value = ArticleDOField.CREATED_TIME, fill = FieldFill.INSERT)
    private Date createdTime;

    @TableField(value = ArticleDOField.UPDATED_TIME, fill = FieldFill.INSERT_UPDATE)
    private Date updatedTime;

    @TableField(value = ArticleDOField.DELETED)
    @TableLogic(value = "0", delval = "1")
    private Integer deleted;

    @TableField(value = ArticleDOField.AUTHOR_ID)
    private String authorId;

    @TableField(value = ArticleDOField.TITLE)
    private String title;

    @TableField(value = ArticleDOField.CONTENT)
    private String content;

    @TableField(value = ArticleDOField.STATUS)
    private Integer status;

    @TableField(value = ArticleDOField.READ_COUNT)
    private Integer readCount;

    public static ArticleVO to(ArticleDO articleDO, String author) {
        ArticleVO articleVO = new ArticleVO();
        BeanUtils.copyProperties(articleDO, articleVO, "author");
        articleVO.setAuthor(author);
        return articleVO;
    }

    public static ArticleInfoVO toArticleInfoVO(ArticleDO articleDO) {
        ArticleInfoVO articleInfoVO = new ArticleInfoVO();
        BeanUtils.copyProperties(articleDO, articleInfoVO);
        return articleInfoVO;
    }

    public static List<ArticleInfoVO> to(Page<ArticleDO> articleDOPage) {
        return articleDOPage.convert(ArticleDO::toArticleInfoVO).getRecords();
    }

    public static ArticleUserInfoVO toArticleUserInfoVO(ArticleDO articleDO) {
        ArticleUserInfoVO articleUserInfoVO = new ArticleUserInfoVO();
        BeanUtils.copyProperties(articleDO, articleUserInfoVO);
        return articleUserInfoVO;
    }

    public static List<ArticleUserInfoVO> toArticleUserInfoVOList(Page<ArticleDO> articleDOPage) {
        return articleDOPage.convert(ArticleDO::toArticleUserInfoVO).getRecords();
    }

    public static ArticleDO from(EditDTO editDTO) {
        return ArticleDO.builder()
                .id(editDTO.getId())
                .title(editDTO.getTitle())
                .content(editDTO.getContent())
                .deleted(editDTO.getDeleted())
                .build();
    }

    public static ArticleDO from(PublishDTO publishDTO, String authorId) {
        return ArticleDO.builder()
                .title(publishDTO.getTitle())
                .content(publishDTO.getContent())
                .authorId(authorId)
                .build();
    }

    public static ArticleDO from(ArticleDTO articleDTO) {
        return ArticleDO.builder()
                .id(articleDTO.getId())
                .title(articleDTO.getTitle())
                .content(articleDTO.getContent())
                .status(articleDTO.getStatus())
                .readCount(articleDTO.getReadCount())
                .deleted(articleDTO.getDeleted())
                .createdTime(articleDTO.getCreatedTime())
                .build(); // TODO how to copy
    }

}
